import Vue from "vue";
import Vuex from "vuex";
import { analyzeContent } from "../api";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    currentPdf: null,
    currentPage: 1,
    totalPages: 0,
    analysisResults: [],
    loading: false,
  },
  mutations: {
    SET_CURRENT_PDF(state, pdf) {
      state.currentPdf = pdf;
    },
    SET_CURRENT_PAGE(state, page) {
      state.currentPage = page;
    },
    SET_TOTAL_PAGES(state, total) {
      state.totalPages = total;
    },
    SET_ANALYSIS_RESULTS(state, results) {
      state.analysisResults = results;
    },
    SET_LOADING(state, status) {
      state.loading = status;
    },
  },
  actions: {
    async uploadPdf({ commit }, file) {
      commit("SET_LOADING", true);
      try {
        // TODO: 实现PDF上传逻辑
        commit("SET_CURRENT_PDF", file);
      } finally {
        commit("SET_LOADING", false);
      }
    },
    async analyzePdfContent({ commit }, content) {
      commit("SET_LOADING", true);
      try {
        const results = await analyzeContent(content);
        commit("SET_ANALYSIS_RESULTS", results);
      } catch (error) {
        console.error("分析失败:", error);
        commit("SET_ANALYSIS_RESULTS", []);
      } finally {
        commit("SET_LOADING", false);
      }
    },
  },
});
